package mn.joker.androidgames;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View.OnKeyListener;
import android.widget.TextView;

public class AccelometerTest extends Activity implements SensorEventListener {
	StringBuilder builder = new StringBuilder();
	TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		textView = new TextView(this);
		textView.setText("Press keys (if you have some)!");
		setContentView(textView);

		SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

		if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0)
			textView.setText("No Accelerometer installed.");
		else {
			Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
			if(!manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME))
				textView.setText("Couldn't register sensor listener.");
			else
				textView.setText("Successfully register sensor listener.");
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		builder.setLength(0);
		builder.append("\nx: ");
		builder.append(event.values[0]);
		builder.append("\ny: ");
		builder.append(event.values[1]);
		builder.append("\nz: ");
		builder.append(event.values[2]);
		textView.setText(builder.toString());
	}

}
